/*
 *  Axolotl - Alternate persistence for Grails
 *  Copyright (C) 2008  Juanjo García Latre
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package com.googlecode.axolotl.jpa;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.FlushModeType;
import javax.persistence.LockModeType;
import javax.persistence.Query;
import javax.persistence.RollbackException;

import org.codehaus.groovy.grails.commons.GrailsApplication;
import org.springframework.transaction.TransactionStatus;

import com.googlecode.axolotl.Database;
import com.googlecode.axolotl.grails.GrailsDomainUtils;
import com.googlecode.axolotl.lazy.LazyEntity;


public class AxolotlEntityManager implements EntityManager {

	private Database database;
	private GrailsApplication application;


	public void persist(Object entity) {
		Object id = GrailsDomainUtils.getIdentifierValue(application, entity);
		
		if (id == null) {
			database.insert(entity);
		}
		else {
			database.update(entity);
		}
	}
	

	public <T> T merge(T entity) {
		throw new UnsupportedOperationException();
	}


	public void remove(Object entity) {
		database.delete(entity);
	}

	
	public <T> T find(Class<T> entityClass, Object id) {
		return (T) database.load(entityClass, id);
	}

	
	public <T> T getReference(Class<T> entityClass, Object id) {
		return (T) LazyEntity.create(entityClass, id, database);
	}


	public void flush() {
		throw new UnsupportedOperationException();
	}


	public FlushModeType getFlushMode() {
		// TODO Auto-generated method stub
		return null;
	}

	
	public void setFlushMode(FlushModeType flushModeType) {
		// TODO Auto-generated method stub
	}
	

	public void lock(Object entity, LockModeType lockMode) {
		// TODO Auto-generated method stub
	}


	public void refresh(Object entity) {
		// TODO Auto-generated method stub
	}


	public void clear() {
		// TODO Auto-generated method stub
	}

	
	public boolean contains(Object entity) {
		return false;
	}


	public Query createQuery(String query) {
		return null;
	}


	public Query createNamedQuery(String arg0) {
		return null;
	}
	

	public Query createNativeQuery(String arg0) {
		return null;
	}

	
	public Query createNativeQuery(String arg0, Class arg1) {
		return null;
	}

	
	public Query createNativeQuery(String arg0, String arg1) {
		return null;
	}


	public void joinTransaction() {
	}

	
	public Object getDelegate() {
		return null;
	}

	
	public void close() {
	}


	public boolean isOpen() {
		return false;
	}

	
	public EntityTransaction getTransaction() {
		return new EntityTransactionImpl();
	}
	
	
	private class EntityTransactionImpl implements EntityTransaction {
		private TransactionStatus status;
		private boolean rollbackOnly;
		
		public void begin() {
			status = database.beginTransaction();	
		}

		public void commit() {
			if (rollbackOnly) {
				throw new RollbackException();
			}
			database.commit(status);
			status = null;
		}

		public void rollback() {
			database.rollback(status);
			status = null;
		}

		public boolean isActive() {
			return status != null;
		}

		public boolean getRollbackOnly() {
			return rollbackOnly;
		}

		public void setRollbackOnly() {
			rollbackOnly = true;
		}		
	}
}
